package com.wh.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.wh.service.IExt.ISysUserPowerService;
import com.legend.utils.*;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.wh.beans.PowerVC;
import com.wh.dao.ext.SysUserPowerDao;
import com.wh.model.BaseModel;
import com.wh.model.SysUserPower;

public class SysUserPowerService extends BaseServiceImp implements ISysUserPowerService {
    Logger log = Logger.getRootLogger();

    private SysUserPowerService() {
    }

    //volatile修饰的写变量不能和之前的读写代码调整，读变量不能和之后的读写代码调整
    private volatile static ISysUserPowerService instance = null;

    /**
     * 单例
     *
     * @return
     */
    public static ISysUserPowerService getInstance() {
        if (instance == null) {
            synchronized (ISysUserPowerService.class) {
                if (instance == null) {
                    instance = new SysUserPowerService();
                }
            }
        }
        return instance;
    }

    @Override
    public ReturnResult insertByModel(BaseModel model) {
        ReturnResult result = new ReturnResult();
        SqlSession session = getSqlSession();
        SysUserPowerDao dao = session.getMapper(SysUserPowerDao.class);
        try {
            model.setId(StringUtils.getGuid());
            dao.insertByModel(model);
        } catch (Exception ex) {
            log.error(ex.getMessage());
        } finally {
            session.close();
        }
        return result;
    }

    /**
     * 保存用户直属操作权限
     *
     * @param lst
     * @return
     */
    public ReturnResult saveUserPower(List<PowerVC> lst) {
        SqlSession session = getSqlSession();
        ReturnResult result = new ReturnResult();
        SysUserPowerDao dao = session.getMapper(SysUserPowerDao.class);
        SysUserPower model = new SysUserPower();
        try {
            for (PowerVC vc : lst) {
                model = new SysUserPower();
                model.setUserId(vc.getUserId());
                model.setModuleCode(vc.getMc());
                List<SysUserPower> tmpLst = dao.selectByModel(model);
                if (tmpLst.size() == 1) {
                    model = tmpLst.get(0);
                    if (!vc.getPv().equals(model.getPowerValue())) {
                        model.setPowerValue(vc.getPv());
                        model.setUpdateDate(new Date());
                        dao.editByModel(model);
                    }
                } else if (tmpLst.size() > 1) {
                    dao.delByModel(model);
                    model.setPowerValue(vc.getPv());
                    model.setId(StringUtils.getGuid());
                    dao.insertByModel(model);
                } else {
                    model.setPowerValue(vc.getPv());
                    model.setId(StringUtils.getGuid());
                    dao.insertByModel(model);
                }
            }
            result.setMsg(i18nUtils.getVal("100001000"));
            result.setStatus(Status.SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            result.setMsg(i18nUtils.getVal("100002000"));
            result.setStatus(Status.ERROR);
        }
        return result;
    }

    @Override
    public ReturnResult editByModel(BaseModel model) {
        ReturnResult result = new ReturnResult();
        SqlSession session = getSqlSession();
        SysUserPowerDao dao = session.getMapper(SysUserPowerDao.class);
        try {
            dao.editByModel(model);
        } catch (Exception ex) {
            log.error(ex.getMessage());
        } finally {
            session.close();
        }
        return result;
    }

    @Override
    public ReturnResult delByMap(Map<String, Object> map) {
        ReturnResult result = new ReturnResult();
        SqlSession session = getSqlSession();
        SysUserPowerDao dao = session.getMapper(SysUserPowerDao.class);
        try {
            dao.delByMap(map);
        } catch (Exception ex) {
            log.error(ex.getMessage());
        } finally {
            session.close();
        }
        return result;
    }

    /**
     * 比较当前用户的与要修改的用户角色关系
     *
     * @param userId 要修改的用户ID
     * @return
     */
    public ReturnResult compareUserRole(String userId, List<String> subRoleLst, String[] editLst) {
        ReturnResult result = new ReturnResult();
        if (editLst.length > subRoleLst.size()) {
            result.setStatus(Status.ERROR);
            // result.setMsg("不能修改比自己角色更多的用户角色！");
            result.setMsg(i18nUtils.getVal("200001008"));
            return result;
        } else {
            for (String ei : editLst) {
                if (subRoleLst.contains(ei))
                    continue;
                else {
                    result.setStatus(Status.ERROR);
                    result.setMsg(i18nUtils.getVal("200001009"));
                    return result;
                }
            }
        }
        result.setStatus(Status.SUCCESS);
        return result;
    }

    /**
     * 返回当前用户拥有的权限
     *
     * @param userId 用户ID
     * @return 返回当前用户拥有的权限
     */
    public ReturnResult userPower(String userId) {
        ReturnResult result = new ReturnResult();
        SqlSession session = getSqlSession();
        SysUserPowerDao dao = session.getMapper(SysUserPowerDao.class);
        SysUserPower entity = new SysUserPower();
        entity.setUserId(userId);
        List<SysUserPower> lst = dao.selectByModel(entity);
        result.setStatus(Status.SUCCESS);
        result.setRows(lst);
        session.close();
        return result;
    }

    @Override
    public ReturnResult selectById(String id) {
        // TODO Auto-generated method stub
        return null;
    }
}
